import React from 'react';
import { observer, inject } from 'mobx-react';
import ViewResourceStore from "../../stores/list/ViewResourceStore";
import ViewResourceContainer from "./ViewResourceContainer";
import ViewResourceContainerMobile from "./ViewResourceContainerMobile";
import { listStore } from "../../stores/list";

export interface ViewResourceProps {
    cubeViewResource: ViewResourceStore,
    Search: listStore;
    height?: number
}

@inject('cubeViewResource', "Search")
@observer
export default class ViewResouce extends React.Component<ViewResourceProps, any> {
    componentDidMount() {
        const { bywhat } = this.props["location"].query;
        this.judgeUrlContainByWhat(bywhat);
    }
    componentWillReceiveProps(n, s) {
        const bywhat_record = this.props["location"].query.bywhat;
        const { bywhat } = n["location"].query;
        if (bywhat || bywhat_record) {
            if (bywhat !== bywhat_record) {
                this.judgeUrlContainByWhat(bywhat);
            }
        }
    }

    judgeUrlContainByWhat = (bywhat: any) => {
        if (!bywhat) bywhat = 0;
        const { cubeViewResource } = this.props;
        cubeViewResource.resourceParams.bywhat = Number(bywhat);
        cubeViewResource.hasUrlByWhat = true;
    }

    getUrlKey(name) {//判断页面url地址是否包含ismobile，并返回值。
            let reg = new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href);
            if(reg && reg!=undefined){
                return decodeURIComponent((reg || [, ""])[1].replace(/\+/g, '%20')) || null           
            }
    }

    render() {
        const { cubeViewResource, Search } = this.props;
        const location = this.props['location'];
        if (location && location.query) {
            if (location.query.sqlwhere) {
                cubeViewResource.sqlwhere = location.query.sqlwhere;
            }
        }

        if(this.getUrlKey('ismobile')=='1'){
            return <ViewResourceContainerMobile cubeViewResource={cubeViewResource} Search={Search} />
        }else{
            return <ViewResourceContainer cubeViewResource={cubeViewResource} Search={Search} />
        }
    }
}